package com.ocm.burnningstar.Adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.blankj.utilcode.util.ScreenUtils;
import com.ocm.burnningstar.Model.User;
import com.ocm.burnningstar.R;
import com.ocm.burnningstar.Utils.Helper;

import org.xutils.common.Callback;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

import static com.ocm.burnningstar.GlobalApplication.typeFaceNum;
import static com.ocm.burnningstar.GlobalApplication.typeFaceTxt;

/**
 * Created by ocm on 2018-01-29.
 */

public class HorUserWaitAdapter extends BaseAdapter {
    List<User> users = new ArrayList<>();
    private Context _context;

    public HorUserWaitAdapter(Context context, List<User> list){
        _context = context;
        users = list;
    }

    @Override
    public int getCount() {
        return users.size();
    }

    @Override
    public Object getItem(int position) {
        return users.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        int screenWidth = ScreenUtils.getScreenWidth();
        int screenHeight = (int)(ScreenUtils.getScreenHeight()*1.2);
        int viewWidth = (int)(screenWidth*0.3);
        int viewHeight = screenHeight/4;
        if( convertView == null ) {
            LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
            convertView = infalInflater.inflate( R.layout.hor_item, null );
//            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(viewWidth,viewHeight);
//            layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
            AbsListView.LayoutParams params = new AbsListView.LayoutParams(viewWidth,viewHeight);
            if(position==0||position==2){
            }else{
                convertView.setTranslationX(15f);
            }
            convertView.setLayoutParams(params);
        }
        final CircleImageView header = (CircleImageView) convertView.findViewById(R.id.header);
        TextView name = (TextView) convertView.findViewById(R.id.name);
        TextView heartRate = (TextView) convertView.findViewById(R.id.rate);
        TextView ratetip = (TextView) convertView.findViewById(R.id.ratetip);

        name.setTypeface(typeFaceTxt);
        ratetip.setTypeface(typeFaceTxt);
        heartRate.setTypeface(typeFaceNum);
        User user = users.get(position);
        name.setText(user.getName());
        heartRate.setText(String.valueOf(user.getHeartRate()));
        ImageOptions imageOptions= new ImageOptions.Builder().setFadeIn(true).build();
        x.image().loadDrawable(user.getHeaderStr(), imageOptions, new Callback.CacheCallback<Drawable>() {
            @Override
            public boolean onCache(Drawable result) {
                header.setImageBitmap(Helper.drawableToBitmap(result));
                return true;
            }

            @Override
            public void onSuccess(Drawable result) {
                Log.i("onBindViewHolder","onSuccess");
                header.setImageBitmap(Helper.drawableToBitmap(result));
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.i("onBindViewHolder","onError");
                ex.printStackTrace();
            }
            @Override
            public void onCancelled(CancelledException cex) {
                Log.i("onBindViewHolder","onCancelled");
                cex.printStackTrace();
            }
            @Override
            public void onFinished() {            }
        });
        ViewGroup.LayoutParams hs = header.getLayoutParams();
        ViewGroup.LayoutParams ns = name.getLayoutParams();
        ns.width =hs.width = (int)(screenWidth*0.07);
        hs.height = hs.width;
        header.setLayoutParams(hs);
//imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

        return convertView;
    }
}
